package com.sp.app.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.sp.app.constans.Constans;
import com.sp.app.domain.Notice;
import com.sp.app.domain.Power;
import com.sp.app.domain.Role;
import com.sp.app.domain.User;
import com.sp.app.service.NoticeService;
import com.sp.app.service.PowerService;
import com.sp.app.service.RoleService;
import com.sp.app.service.UserService;
import com.sp.app.util.DateUtil;
import com.sp.app.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;


@Controller
@RequestMapping("/User")
public class UserController {

	@Autowired
	private UserService userService;

	@Autowired
	private PowerService powerService;

	@Autowired
	private RoleService roleService;

	@Autowired
	private NoticeService noticeService;

	@RequestMapping("/Main")
	public String res(HttpServletRequest request){
		String time = DateUtil.getStringToday();
		request.getSession().setAttribute("time", time);

		Notice notice = new Notice();

		List<Notice> list = noticeService.queryAll(notice);

		notice = list.get(0);

		User user = userService.selectByPrimaryKey(notice.getUserid());

		notice.setUserid(user.getName());
		request.getSession().setAttribute("notice", notice);

		return "Main";
	}

	@RequestMapping("/changePa")
	public String res1(){
		return "changePass";
	}


	@RequestMapping("/Login")
	public ModelAndView login(HttpServletRequest request,String id,String password) throws Exception{

		ModelAndView mav = new ModelAndView();

		User user1 = userService.selectByPrimaryKey(id);

		if(user1 == null || !password.equals(user1.getPassword())){
			mav.setViewName("index");
		    request.getSession().setAttribute("info", "error");
			return mav;
		}else{
			Role role = new Role();
			role.setRoleid(user1.getRoleid());
			List<Role> list =roleService.QueryAll(role);
			role =list.get(0);
			request.getSession().setAttribute("roleName", role.getRolename());
			Power power = powerService.selectByPrimaryKey(role.getPowerid());

			if(!StringUtil.isNullOrEmpty(power.getPower())){
				request.getSession().setAttribute("power", power.getPower());
			}

			String time = DateUtil.getStringToday();
			request.getSession().setAttribute("time", time);
			request.getSession().setAttribute("user", user1);
			mav.setViewName("redirect:/User/Main");
		}
		return mav;
	}

	@RequestMapping("/updateStudent")
	public String update(User user){
		userService.updateByPrimaryKey(user);
		return "redirect:/User/student";
	}

	@RequestMapping("/updateTeacher")
	public String updatet(User user){
		userService.updateByPrimaryKey(user);
		return "redirect:/User/teacher";
	}

	@RequestMapping("/updatePa")
	public String updatePa(String userID,String password){
		User user = new User();
		System.out.println(userID);
		User user1 = userService.selectByPrimaryKey(userID);
		user1.setPassword(password);
		userService.updateByPrimaryKey(user1);
		return "Main";
	}

	@RequestMapping("/delete")
	public String delete(String ID){
		userService.deleteByPrimaryKey(ID);
		return "redirect:/User/queryAll";
	}

	@RequestMapping("/teacher")
	public String QueryAllTeacher(HttpServletRequest request,User user){

		List<User> list = userService.QueryAllTeacher(user);
		request.setAttribute("list", list);

	   if(null != user.getName()){
			request.setAttribute("name", user.getName());
		}
	   if(null != user.getMobile()){
			request.setAttribute("mobile", user.getMobile());
		}
		return "teacher";
	}


	@ResponseBody
	@RequestMapping("/jsonteacher")
	public String QueryAllTeacherjson(HttpServletRequest request,User user){

		List<User> list = userService.QueryAllTeacher(user);
		JSONObject json = new JSONObject();

		return json.toJSONString(list);
	}

	@RequestMapping("/student")
	public String QueryAllStudent(HttpServletRequest request,User user){

		List<User> list = userService.QueryAllStudent(user);
		request.setAttribute("list", list);

		if(null != user.getName()){
			request.setAttribute("name", user.getName());
		}
		if(null != user.getMobile()){
			request.setAttribute("mobile", user.getMobile());
		}
		return "student";
	}

	@RequestMapping("/addteacher")
	public String  addUser(User user){

	    String passWord = "123456";
		user.setPassword(passWord);
		user.setType(Constans.TEACHER);
		userService.insert(user);
		return "redirect:/User/teacher";
	}

	@RequestMapping("/addstudent")
	public String  addStudent(User user){

		String passWord = "123456";
		user.setPassword(passWord);
		user.setType(Constans.STUDENT);
		userService.insert(user);
		return "redirect:/User/student";
	}

	@ResponseBody
	@RequestMapping("/queryOne")
	public String queryOne(String ID){
		User user = new User();
		user.setId(ID);
		List<User> list = userService.QueryAll(user);
		user = list.get(0);
		JSONObject json = new JSONObject();
		String data = json.toJSONString(user);

		return data;
	}

	@RequestMapping("/quit")
	public ModelAndView quit(HttpServletRequest request) throws Exception{
		ModelAndView mav = new ModelAndView();
		HttpSession session1 = request.getSession();
		session1.invalidate();
		request.getSession().setAttribute("info", "quit");
		mav.setViewName("index");

		return mav;
	}



}
